define(
		["dojo/_base/declare",
		 "dojo/dom",
         "dojo/on",
         "dojo/dom-construct",
         "gistech/widgets/baseWidget/BaseWidget",
         "gistech/widgets/normalDialog/NormalDialog",
         "dojo/text!./templates/layerLable.html",
         "dojo/_base/array",
         "dojo/dom-style",
         "dojo/dom-construct",
         "dojo/_base/lang",
         "dojo/topic",
		 ],function(declare,dom,on,domConstrut,BaseWidget,NormalDialog,template,array,domStyle,domConstrut,lang,topic){
			return declare([BaseWidget],{
				templateString:template,
				containerDiv:null,
				currentNum:0,
				map:null,
				mid:"",
				containerDivID:"",
				containerUlDom:null,
				LayerLoadedOrError:null,
				labelDivMaxWidth:0,
				srcdiv:null,
				treeNode:null,
				constructor:function(args){
					this.inherited(arguments);
					this.map=args.map;
					this.mid = args.mid;
					this.containerDiv=dojo.byId(args.id);
					this.containerDivID = args.id;
					this.treeNode = args.treeNode;
				},
				postCreate:function(){
					this.inherited(arguments);
				},
				startup:function(){
//					if((Global.maps.length==0||Global.maps.length==1&&Global.activeMapId=="map")||(Global.maps.length==4&&Global.activeMapId=="map3")){
//						if(Global.widgets.left.hasExpand==true){
//							$(this.domNode).css("left",(Global.widgets.left.panelWidth+20)+"px");
//						}
//					}
					if((Global.maps.length==0||Global.maps.length==1&&Global.activeMapId=="map")){
						if(Global.widgets.left.hasExpand==true){
							$(this.domNode).css("left",(Global.widgets.left.panelWidth+20)+"px");
						}
					}
					this.inherited(arguments);
					this.addEvent();
					this.addLable(this.treeNode);
				},
				addEvent:function(){
					on(this.expand,"click",function(e){
						$(this).toggleClass("glyphicon-chevron-right");
						$(this).toggleClass("glyphicon-chevron-down");
						if($(this).hasClass("glyphicon-chevron-right")){
							$(this).parent("div").find(".liContainer").animate({"height":"30px"},"slow");
						}else{
							var heigth=$(this).parent("div").find(".liContainer").find("li").length*32;
							$(this).parent("div").find(".liContainer").animate({"height":"100%"},"slow");
						}});
					debugger;
					if(this.LayerLoadedOrError==null){
						this.LayerLoadedOrError = topic.subscribe("LayerLoadedOrErrorHandler",lang.hitch(this,this.LayerLoadedOrErrorHandler));
					}
					
				},
				LayerLoadedOrErrorHandler:function(layerInfo){
					debugger;
					var labelLi = $("#"+this.containerDivID).children(".DIVcontainer").find("li#"+layerInfo.id.split("#")[0]).children(".maplayerloading-indicator");
					if(labelLi){
						labelLi.remove();
						var colorstr = "#000";
						if(!layerInfo.loaded){
							colorstr = "#f00";
						}
						$("#"+this.containerDivID).children(".DIVcontainer").find("li#"+layerInfo.id.split("#")[0]).children(".title").css("color",colorstr);
					}
				},
				deleteLableAndRemoveLayer:function(mapDivId,lableId){
					$("#"+mapDivId).children(".DIVcontainer").find("li#"+lableId).remove();
					var dyLayer=this.map.getLayer(lableId+"#M");
					if(dyLayer){
						this.map.removeLayer(dyLayer);
					}
					var feLayer=this.map.getLayer(lableId+"#F");
					if(feLayer){
						this.map.removeLayer(feLayer);
					}
					if(Global.mapshasaddlayers.hasOwnProperty(this.mid)){
						for(var i=0;i<Global.mapshasaddlayers[this.mid].length;i++){
							var item = Global.mapshasaddlayers[this.mid][i];
							if(item.layerObj.RESOURCE_ID==lableId){
								Global.mapshasaddlayers[this.mid].splice(i, 1);
								break;
							}
						}
					}
					if($(this.containerDiv).find("li").length<=2){
						$(this.containerDiv).find(".arrowRight").css("display","none");
                    }
					//当前分屏中所有图层全部移除是销毁标签组件
					if($("#"+mapDivId).children(".DIVcontainer").find("li").length<=1){
						this.destroy();
					}
				
				},
				addLable:function(treeNode){
					var cloneLi=$(this.domNode).find(".demoLi").clone()[0];
					var titlelen = treeNode.text.length*16+76;
					if(this.labelDivMaxWidth<titlelen){
						this.labelDivMaxWidth = titlelen;
						$("#"+this.containerDivID).find(".DIVcontainer").css({"width":titlelen+"px"});
						var a = dojo.query("#"+this.containerDivID+".DIVcontainer");
						domStyle.set(a,"width",titlelen+"px");
					}
					
					//a.css("width",titlelen+"px");
					$(cloneLi).attr("id",treeNode.id);
					$(cloneLi).css({"display":"block","line-height":"30px","width": "100%","border":"1px solid #b5bcc7","padding-left":"10px","text-align":"center"});
					$(cloneLi).find(".title")[0].innerHTML=treeNode.text;
                    domConstrut.place(cloneLi,$(this.domNode).find(".liContainer")[0], "first");
                    domConstrut.place(this.domNode,this.containerDiv);
                    $(cloneLi).find("#lydelete").bind("click",lang.hitch(this,function(e){
                    	 this.deleteLableAndRemoveLayer($(this)[0].id,e.currentTarget.parentNode.id);
                    }));
                    if($(this.containerDiv).find("li").length>2){
                    	$(this.containerDiv).find(".arrowRight").css("display","block");
                    }
//                    $(cloneLi).bind("click",function() {
//                        if($(this).next())
//                            $(this).next().after($(this));
//                    });
                    if(this.containerUlDom==null){
                    	this.containerUlDom  = $("#"+this.containerDivID).children(".DIVcontainer").find("ul" );
                    	this.containerUlDom.sortable({
                    		revert: true
                    	});
                    	this.containerUlDom.bind('sortstop',lang.hitch(this,function(e,ui) {
                    		this.reOrderLayer();
                    	}));
                    }
                    
                    //$(this.containerDiv).find("li").disableSelection();
                    
                    $(cloneLi).find(".title").bind("dblclick",lang.hitch(this,function(e){
                    	this.showOptialSet(e.currentTarget.parentNode);
                    }));
				},
				reOrderLayer:function(){
	            	try{
	            		var treeLayerCount=$(this.containerDiv).find("li").length-1;//3个
	                	var layerOrder=$(this.containerDiv).find("li") ;//目前加载的图层的顺序，序号小的在上层
	                	dojo.forEach(layerOrder,lang.hitch(this,function(item,index){
	                		if(item&&item.id&&item.id!=""){
	                			var fl = this.map.getLayer(item.id+"#F");
	                			if(fl){
	                				this.map.reorderLayer(fl,treeLayerCount-index);
	                			}
	                			var ml = this.map.getLayer(item.id+"#M");
	                			if(ml){
	                				this.map.reorderLayer(ml,treeLayerCount-index); 
	                			}
	                		}
	                	}));
	                	this.map.reorderLayer(this.map.getLayer("highLightLayer"),2*treeLayerCount+1);
	            	}catch (e) {
						console.log(e);
					}finally{
						
					}
	            },
				//设置图层显隐性。
	            showOptialSet:function(treeNode){
	            	var dyLayer=this.map.getLayer(treeNode.id+"#M");
	            	var feLayer=this.map.getLayer(treeNode.id+"#F");
	            	var opcitalF=feLayer.opacity*10;
	            	opcitalStr="<input type='range' min='0' max='10'  step='1' value='"+opcitalF+"'  id='"+treeNode.id+"'>"
	            	artDialog({id:treeNode.id,title:treeNode.innerText+'图层设置',content:opcitalStr,lock:false, style:'succeed noClose',noText:null,},function(){},function(){});
	            	var range=dojo.byId(treeNode.id);
	            	on(range,"mouseup",lang.hitch(this,this.setOptail));
	            },
	            setOptail:function(evt){
	            	var dyLayer=this.map.getLayer(evt.target.id+"#M");
	            	dyLayer.setOpacity(parseInt(evt.target.value)/10);
	            	var feLayer=this.map.getLayer(evt.target.id+"#F");
	            	feLayer.setOpacity(parseInt(evt.target.value)/10);
	            }
			})
		 })